using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FeedAntGame.Control;
using FeedAntGame.Sprite.Event;
using FeedAntGame.Sprite;


namespace FeedAntGame.Component.Dialog
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class PlayAgainDlg : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Texture2D bg;
        SpriteBatch spriteBatch;
        SpriteFont font;
        Button playAgainButton;
        Button mainMenuButton;
        Vector2 bgPos;
        string value;
        Vector2 valuePos;
        public string Value
        {
            get { return this.value; }
            set { this.value = value; }
        }

        List<BaseControl> controlList = new List<BaseControl>();
        public PlayAgainDlg(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
          
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
 

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            font = Game.Content.Load<SpriteFont>(@"Fonts\score");
            //bg
            bg = Game.Content.Load<Texture2D>(@"Images\message_background");
            bgPos = new Vector2(Game.Window.ClientBounds.Width / 2 - bg.Width / 2,
                Game.Window.ClientBounds.Height / 2 - bg.Height / 2 - 25);
            //value 
            value = "Do you want to \n  play again ?";
            valuePos = bgPos + new Vector2(90, 90);
            //play again button
            playAgainButton = new Button(Game, Button.TYPE.PLAY_AGAIN, bgPos + new Vector2(200, 215));
            EventSprite playAgainSprite = (EventSprite)playAgainButton.Sprite;
            playAgainSprite.Click += new EventSprite.ClickButton(onPlayAgainClick);
            playAgainSprite.Hover += new EventSprite.HoverButton(onShowHand);
            playAgainSprite.Scale = 1.5f;
            controlList.Add(playAgainButton);
            //play again button
            mainMenuButton = new Button(Game, Button.TYPE.MAIN_MENU, bgPos + new Vector2(25, 217));
            EventSprite mainMenuSprite = (EventSprite)mainMenuButton.Sprite;
            mainMenuSprite.Click += new EventSprite.ClickButton(onMainMenuClick);
            mainMenuSprite.Hover += new EventSprite.HoverButton(onShowHand);
            mainMenuSprite.Scale = 1.15f;
            controlList.Add(mainMenuButton);
            base.LoadContent();
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            for (int i = 0; i < controlList.Count; i++)
            {
                BaseControl control = controlList.ElementAt(i);
                control.Sprite.Update(gameTime, Game.Window.ClientBounds);
                if ((control.Sprite as EventSprite).IsPlaySoundHover())
                {
                    ((GameFA)Game).PlayCue(control.Sprite.collisionCueName);
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            // Draw background
            spriteBatch.Draw(bg, new Rectangle((int)bgPos.X, (int)bgPos.Y, bg.Width, bg.Height), Color.White);

            //Draw controls
            foreach (BaseControl c in controlList)
            {
                c.Sprite.Draw(gameTime, spriteBatch);
            }

            //Draw message
            spriteBatch.DrawString(font, value, valuePos, Color.White);

            spriteBatch.End();
            base.Draw(gameTime);
        }

        private void onPlayAgainClick()
        {
            Finish();
            PlayManager playManager = ((GameFA)Game).playManager;
            playManager.Enabled = false;
            playManager.SetNewGame();
            ((GameFA)Game).OpenPickCandy();
            this.Enabled = false;
            playManager.ResetCandyFeed();
            playManager.TextList.Clear();
            playManager.gameState = GameState.READY;
        }
        private void onMainMenuClick()
        {
            Finish();
            ((GameFA)Game).GoToMainMenu();
        }
        private void onShowHand()
        {
            ((GameFA)Game).Cursor.State = Cursor.STATE_TYPE.HAND;
        }

        public void Start()
        {
            this.Enabled = true;
            this.Visible = true;
            ((GameFA)Game).Volume(0, "Music");
            for (int i = 0; i < Game.Components.Count; i++)
            {
                IGameComponent component = Game.Components.ElementAt(i);
                DrawableGameComponent c = (DrawableGameComponent)component;
                if (!(c is PlayAgainDlg) && c.Visible == true && c.Enabled == true)
                {
                    c.Enabled = false;
                }
            }
        }

        private void Finish()
        {
            this.Enabled = false;
            this.Visible = false;
            for (int i = 0; i < Game.Components.Count; i++)
            {
                IGameComponent component = Game.Components.ElementAt(i);
                DrawableGameComponent c = (DrawableGameComponent)component;
                if (!(c is PlayAgainDlg) && c.Visible == true && c.Enabled == false)
                {
                    c.Enabled = true;
                }
            }
            ((GameFA)Game).Volume(1, "Music");
        }
    }
}
